using MediatR;
using Microsoft.AspNetCore.Mvc;
using Medical.ErrorCode;
using BaseData.ErrorCode;
using Medical.ReadApi.Appcontext.Command;
using Medical.ReadApi.Dto;

namespace Medical.ReadApi.Controllers
{
    /// <summary>
    /// 挂号单管理控制器（读操作）
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ManagementController : ControllerBase
    {
        private readonly IMediator _mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">MediatR中介者</param>
        public ManagementController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 根据ID获取挂号单模板详情
        /// </summary>
        /// <param name="templateId">模板ID</param>
        /// <returns>模板详细信息</returns>
        /// <remarks>
        /// 示例请求:
        /// GET /api/Management/GetById?templateId=1
        /// </remarks>
        /// <response code="200">查询成功，返回模板详细信息</response>
        /// <response code="400">请求参数错误</response>
        /// <response code="404">模板不存在</response>
        /// <response code="500">服务器内部错误</response>
        [ProducesResponseType(typeof(ApiResult<RegistrationSlipTemplateDto>), 200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(404)]
        [ProducesResponseType(500)]
        [HttpGet]
        public async Task<ApiResult<RegistrationSlipTemplateDto>> GetById([FromQuery] long templateId)
        {
            try
            {
                var query = new GetRegistrationSlipTemplateByIdQuery
                {
                    TemplateId = templateId
                };
                var result = await _mediator.Send(query);
                return new ApiResult<RegistrationSlipTemplateDto>
                {
                    Code = ApiEnum.查询成功,
                    Message = "查询成功",
                    data = result
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<RegistrationSlipTemplateDto>
                {
                    Code = ApiEnum.查询失败,
                    Message = ex.Message,
                    data = null
                };
            }
        }

        /// <summary>
        /// 获取挂号单模板列表（支持分页和筛选）
        /// </summary>
        /// <param name="isActive">是否启用状态筛选（可选）</param>
        /// <param name="templateNameKeyword">模板名称关键词（模糊搜索，可选）</param>
        /// <param name="page">页码（从1开始，默认值：1）</param>
        /// <param name="size">每页数量（默认值：10）</param>
        /// <returns>分页的模板列表</returns>
        /// <remarks>
        /// 示例请求:
        /// GET /api/Management/GetList?isActive=true&amp;templateNameKeyword=挂号&amp;page=1&amp;size=10
        /// 
        /// 所有查询参数都是可选的，可以任意组合使用。
        /// </remarks>
        /// <response code="200">查询成功，返回分页的模板列表</response>
        /// <response code="400">请求参数错误</response>
        /// <response code="500">服务器内部错误</response>
        [ProducesResponseType(typeof(ApiResult<ApiPaging<RegistrationSlipTemplateDto>>), 200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(500)]
        [HttpGet]
        public async Task<ApiResult<ApiPaging<RegistrationSlipTemplateDto>>> GetList(
            [FromQuery] bool? isActive = null,
            [FromQuery] string? templateNameKeyword = null,
            [FromQuery] int page = 1,
            [FromQuery] int size = 10)
        {
            try
            {
                var query = new GetRegistrationSlipTemplatesListQuery
                {
                    IsActive = isActive,
                    TemplateNameKeyword = templateNameKeyword,
                    Page = page,
                    Size = size
                };
                var result = await _mediator.Send(query);
                return new ApiResult<ApiPaging<RegistrationSlipTemplateDto>>
                {
                    Code = ApiEnum.查询成功,
                    Message = "查询成功",
                    data = result
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<ApiPaging<RegistrationSlipTemplateDto>>
                {
                    Code = ApiEnum.查询失败,
                    Message = ex.Message,
                    data = null
                };
            }
        }
    }
}

